<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<div class="modal-body">
    <form class="form-horizontal" action="" method="post" id="users_input" novalidate="novalidate">
        <input type="hidden" name="keyId" id="keyId" value="<s:property value="user.id" />">

        <fieldset>
            <div class="form-group">
                <label class="col-md-3 control-label" >登录手机号</label>
                <div class="col-md-9">
                    <input class="form-control" name="mobile" id="mobile"  <s:if test="user!=null"> disabled="disabled" </s:if> <s:else>placeholder="请输入登陆手机号"</s:else> type="text" value="<s:property value="user.mobile" />"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label" >姓名</label>
                <div class="col-md-9">
                    <input class="form-control" name="name" id="name" placeholder="请输入姓名"
                           type="text" value="<s:property value="user.name" />">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">帐号类型</label>
                <div class="col-md-9">
                    <label  class="checkbox-inline">
                        <input type="checkbox" id="publicUser" class="checkbox style-0" name="adminType" value="1">
                            <span>前台账户</span>
                        </input>

                    </label>
                    <label  class="checkbox-inline">
                        <input type="checkbox" id="adminUser" class="checkbox style-0" name="adminType" value="2">
                            <span>后台账户</span>
                        </input>
                    </label>
                    <input type="text" id="hideType" hidden value="<s:property value="user.adminType"/> "/>
                </div>

            </div>
            <!-- 编辑时 不显示密码 -->
            <s:if test="user==null">
            <div class="form-group">
                <label class="col-md-3 control-label">密码</label>
                <div class="col-md-9">
                    <input class="form-control" placeholder="密码不为空" name="password" id="password"
                           type="password">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">再输一次密码</label>
                <div class="col-md-9">
                    <input class="form-control" placeholder="再输一次密码" name="repassword" id="repassword"
                           type="password">
                </div>
            </div>
            </s:if>

        </fieldset>
    </form>
</div>

<div class="modal-footer">
    <a role="button" class="btn btn-primary" id="dialog-ok"> <i class="fa fa-save"></i> 提交</a>
    <a href="#" class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</a>
    <%--<a role="button" class="btn btn-default" onclick="gDialog.fClose();">gDialog.fClose();方法关闭弹窗</a>--%>
</div>

<script>
    jQuery.validator.addMethod("mobile", function(value, element) {
        var length = value.length;
        var mobile = /^[1][358]\d{9}$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "手机号码格式错误");


    $("#users_input").validate({
        rules : {
            name : {
                required : true
            },
            mobile : {
                required : true,
                mobile :true,
                remote:{                                          //唯一性检查，手机号检查
                    type:"POST",
                    url:"ajax-users!isUniqueByMobile.action",             //servlet
                    data:{
                        keyId:"",
                        validatePro:"mobile",
                        newValue:function(){return $("#mobile").val();}
                    }
                }
            },
            password : {
                required : true
            },
            repassword : {
                required : true,
                equalTo : '#password'
            }
        },
        messages : {
            name : {
                required : '请输入用户名'
            },
            mobile : {
                required : '请输入您的登陆手机号',
                remote:jQuery.format("该登陆手机号已经被注册")
            },
            password : {
                required : '请输入您的密码'
            },
            repassword : {
                required : '请再输入一次密码',
                equalTo : '两次密码输入应一致'
            }
        },
        errorPlacement : function(error, element) {
            error.insertAfter(element.parent()).css({"color":"red","margin-left":"150px"});
        }
    });

    $("#dialog-ok").unbind("click").bind("click",function(){
        var $valid = $("#users_input").valid();
        if(!$valid) return false;
        var actionUrl = "<%=path%>/com/ajax-users!save.action";
        form_save("users_input",actionUrl);
        gDialog.fClose();
        jQuery("#ajax_users_table").trigger("reloadGrid");
    });
    $(function(){
        var type = $("#hideType").val();
        console.debug(type);
        if(type.indexOf("1") !=-1){
            $("#publicUser").prop("checked",true);
        }
        if(type.indexOf("2") !=-1){
            $("#adminUser").prop("checked",true);
        }
    });
</script>